#include "DoOpArray.h"
#include "DevilSemantic.h"

void DoOpArray::ds_array::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	
	DoArray *p0 = new DoArray();
	int size = toNumber(obj1)->getInt();
	for (int i = 0; i < size; i++) {
		p0->add(_DoObject::mNullObject);
	}
	obj2 = CRefPtr< _DoObject >(p0);
	state.getOpStack().push(obj2);
}

void DoOpArray::ds_left::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpArray::ds_right::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpArray::ds_length::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

bool DoOpArray::ds_length::match(DevilSemantic &state) const 
{
	if (state.getOpStack().size() < 1)
		return false;
	CRefPtr< _DoObject > obj1 = state.getOpStack().top(0);
	if (obj1->type() == eArray)
		return true;
	return false;
}

void DoOpArray::ds_get::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	
	obj3 = toArray(obj2)->get(toNumber(obj1)->getInt());
	state.getOpStack().push(obj3);
}

bool DoOpArray::ds_get::match(DevilSemantic &state) const 
{
	if (state.getOpStack().size() < 2)
		return false;
	CRefPtr< _DoObject > obj1 = state.getOpStack().top(1);
	if (obj1->type() == eArray)
		return true;
	return false;
}

void DoOpArray::ds_put::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj3 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;

	toArray(obj3)->set(toNumber(obj2)->getInt(), obj1);
}

bool DoOpArray::ds_put::match(DevilSemantic &state) const 
{
	if (state.getOpStack().size() < 3)
		return false;
	CRefPtr< _DoObject > obj1 = state.getOpStack().top(2);
	if (obj1->type() == eArray)
		return true;
	return false;
}

void DoOpArray::ds_getinterval::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpArray::ds_putinterval::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

void DoOpArray::ds_astore::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;

	DoArray *array = toArray(obj1);
	for(size_t i = 0; i < array->size(); i++) {
		array->set(array->size() - i - 1, state.getOpStack().pop());
	}
	state.getOpStack().push(obj1);
}

void DoOpArray::ds_aload::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;

	DoArray *array = toArray(obj1);
	for(size_t i = 0; i < array->size(); i++) {
		state.getOpStack().push(array->get(i));
	}
	state.getOpStack().push(obj1);
}

bool DoOpArray::ds_aload::match(DevilSemantic &state) const 
{
	if (state.getOpStack().size() < 1)
		return false;
	CRefPtr< _DoObject > obj1 = state.getOpStack().top(0);
	if (obj1->type() == eArray)
		return true;
	return false;
}

void DoOpArray::ds_copy::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1;
	CRefPtr< _DoObject > obj2;
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	CXXTHROW
}

bool DoOpArray::ds_copy::match(DevilSemantic &state) const 
{
	if (state.getOpStack().size() < 2)
		return false;
	CRefPtr< _DoObject > obj1 = state.getOpStack().top(0);
	CRefPtr< _DoObject > obj2 = state.getOpStack().top(1);
	if (obj1->type() == eArray && obj2->type() == eArray)
		return true;
	return false;
}

void DoOpArray::ds_forall::execute(DevilSemantic &state)
{
	CRefPtr< _DoObject > obj1 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj2 = state.getOpStack().pop();
	CRefPtr< _DoObject > obj3;
	CRefPtr< _DoObject > obj4;
	CRefPtr< _DoObject > obj5;
	
	DoArray *array = toArray(obj2);
	for(size_t i = 0; i < array->size(); i++) {
		obj3 = array->get(i);
		state.getOpStack().push(obj3);
		obj1->execute(state);
	}
}

bool DoOpArray::ds_forall::match(DevilSemantic &state) const 
{
	if (state.getOpStack().size() < 2)
		return false;
	CRefPtr< _DoObject > obj1 = state.getOpStack().top(1);
	if (obj1->type() == eArray)
		return true;
	return false;
}

